import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from "../views/HomeView.vue"


Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect:"/home"
  },
  {
    path: '/home',
    name:"home",
    component:HomeView,
    meta:{
      keep:true
    }
  },
  {
    path: '/start',
    name: 'start',
    component: () => import('../views/gameStart.vue'),
    meta:{
      keep:true
    },
      // 独享路由守卫
      beforeEnter:(to,from,next)=>{
        let token = window.localStorage.getItem("login");
        // 判断是否登录
        if(!token){
          next("/login");
          return;
        }else{
          next();
        }
      }
  },

  {
    path: '/collection',
    name: 'collection',
    component: () => import('../views/collectionView.vue')
  },
  {
    path: '/GameDetails',
    name: 'GameDetails',
    component: () => import('../views/GameDetails.vue'),
    beforeEnter:(to,from,next)=>{
      let token = window.localStorage.getItem("login");
      // 判断是否登录
      if(!token){
        next("/login");
        return;
      }else{
        next();
      }
    }
  },
  {
    path: '/gameCo',
    name: 'gameCo',
    component: () => import('../views/gameCollection.vue'),
      // 独享路由守卫
      beforeEnter:(to,from,next)=>{
        let token = window.localStorage.getItem("login");
        // 判断是否登录
        if(!token){
          next("/login");
          return;
        }else{
          next();
        }
      }
  },
  {
    path: '/gameCont',
    name: 'gameCont',
    component: () => import('../views/contView.vue'),  
    beforeEnter:(to,from,next)=>{
      let token = window.localStorage.getItem("login");
      // 判断是否登录
      if(!token){
        next("/login");
        return;
      }else{
        next();
      }
    }
  },
  {
    path: '/rank',
    name: 'rank',
    component: () => import('../views/rankView.vue')
  },
  {
    path: '/user',
    name: 'user',
    component: () => import('../views/userView.vue'),
  
    
  },
  {
    path: '/class',
    name: 'class',
    component: () => import('../views/GameClass.vue'),
  
    
  },
  {
    path: '/allClass',
    name: 'allClass',
    component: () => import('../views/AllClass.vue'),
    
  },
  {
    path:'/login',
    name:'login',
    component:()=>import("../views/loginView.vue")
  },
  {
    path:'/search',
    name:'search',
    component:()=>import("../views/searchView.vue"),
    beforeEnter:(to,from,next)=>{
      let token = window.localStorage.getItem("login");
      // 判断是否登录
      if(!token){
        next("/login");
        return;
      }else{
        next();
      }
    }
  },
  {
    path:'/enshrine',
    name:'enshrine',
    component:()=>import("../views/enshrineView.vue"),
    beforeEnter:(to,from,next)=>{
      let token = window.localStorage.getItem("login");
      // 判断是否登录
      if(!token){
        next("/login");
        return;
      }else{
        next();
      }
    }
  },
  {
    path:'/searchList',
    name:'searchList',
    component:()=>import("../views/searchList.vue"),
    beforeEnter:(to,from,next)=>{
      let token = window.localStorage.getItem("login");
      // 判断是否登录
      if(!token){
        next("/login");
        return;
      }else{
        next();
      }
    }
  },
  {
    path:'/paly',
    name:'paly',
    component:()=>import("../views/MyPlay.vue"),
    beforeEnter:(to,from,next)=>{
      let token = window.localStorage.getItem("login");
      // 判断是否登录
      if(!token){
        next("/login");
        return;
      }else{
        next();
      }
    }
  },
  {
    path:'/pinkView',
    name:'pinkView',
    component:()=>import("../views/pinkView.vue"),
    beforeEnter:(to,from,next)=>{
      let token = window.localStorage.getItem("login");
      // 判断是否登录
      if(!token){
        next("/login");
        return;
      }else{
        next();
      }
    }
  },
  {
    path: '/new',
    name: 'new',
    component: () => import('../views/newView.vue')
  }
]

// 注意:解决路由跳转问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location){
  return originalPush.call(this,location).catch(err=>err);
}



const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 配置全局路由守卫
// 用户必须登录才进入  必须登录才可以进入
// router.beforeEach((to,from,next)=>{
//   let token = window.localStorage.getItem("token");
//   // 登录界面判断
//   if(to.path == "/login"){
//       next();
//       return;
//   }else{ //判断是否登录
//     if(!token){
//       next("/login");//进入登录页面
//       return;
//     }else{
//       next();//执行下一个路由
//     }
//   }
// })

router.beforeEach((to, from, next) => {    
  // chrome
  document.body.scrollTop = 0
  // firefox
  document.documentElement.scrollTop = 0
  // safari
  window.pageYOffset = 0
  next()
})


export default router
